package people;

import java.io.IOException;
import java.io.Serializable;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

/**
 * The Person class, where all users extend to.
 * 
 * @author Peter
 * @version $Revision: 1.0 $
 */

public abstract class Person implements Serializable {
	/**
	 * Information that all user/people (person) objects should at least contain
	 */
	private String firstName;

	/**
	 * Field lastName.
	 */
	private String lastName;

	/**
	 * Field userID.
	 */
	private String userID;

	/**
	 * Field userPass. User's password
	 */
	private String userPass;

	/**
	 * Field isLoggedIn.
	 */
	private boolean isLoggedIn;

	/**
	 * Field MyLogger.
	 */
	protected static final Logger MYLOGGER = Logger.getLogger("Person");

	static {

		MYLOGGER.setLevel(Level.ALL);
		MYLOGGER.setUseParentHandlers(false);

		try {
			final FileHandler fh = new FileHandler("PersonLog.log");
			MYLOGGER.addHandler(fh);
			final SimpleFormatter formatter = new SimpleFormatter();
			fh.setFormatter(formatter);
		} catch (SecurityException e) {
			MYLOGGER.log(Level.SEVERE,
					"Security Exception creating a file handler", e);
			e.printStackTrace();
		} catch (IOException e) {
			MYLOGGER.log(Level.SEVERE, "IO Exception creating a "
					+ "file handler", e);
			e.printStackTrace();
		}

	}

	/**
	 * Explicitly say whether this object is a patient, nurse, doctor, or system
	 * admin
	 */
	public enum UserType {
		PATIENT, NURSE, DOCTOR, ADMIN
	}

	/**
	 * Field personType
	 */
	private UserType personType;

	/**
	 * Constructor that creates a person with no specifics
	 * 
	 * @param fName
	 *            - first name of person
	 * @param lName
	 *            - last name of person
	 */
	public Person(final String fName, final String lName) {
		this.setFirstName(fName);
		this.setLastName(lName);
		this.isLoggedIn = false;

		final char firstLetter;
		if (fName.isEmpty()) {
			firstLetter = " ".charAt(0);
		} else {
			// Temporary: auto gen username
			firstLetter = fName.charAt(0);
		}
		final String uName = firstLetter + lName; // auto-gen uName prob
		this.setUserID(uName);
		// End: auto gen username
	}

	public Person() {
		this.isLoggedIn = false;
	}

	/**
	 * Sets first name of person
	 * 
	 * @param firstName
	 *            - first name of person
	 */
	public void setFirstName(String firstName) {
		MYLOGGER.info("Setting first name");
		this.firstName = firstName;
	}

	/**
	 * Gets the first name of person
	 * 
	 * @return first name of person
	 */
	public String getFirstName() {
		MYLOGGER.info("Getting first name");
		return firstName;
	}

	/**
	 * Sets the last name of person
	 * 
	 * @param lastName
	 *            - last name of person
	 */
	public void setLastName(String lastName) {
		MYLOGGER.info("Setting last name");
		this.lastName = lastName;
	}

	/**
	 * Gets the last name of person
	 * 
	 * @return last name
	 */
	public String getLastName() {
		MYLOGGER.info("Getting last name");
		return lastName;
	}

	/**
	 * Sets a username for person
	 * 
	 * @param userID
	 *            - person's username
	 */
	public void setUserID(String userID) {
		MYLOGGER.info("Setting username");
		this.userID = userID;
	}

	/**
	 * Gets the username of person
	 * 
	 * @return username
	 */
	public String getUserID() {
		return userID;
	}

	/**
	 * Assert the logged-in status
	 */
	public void loggedIn() {
		MYLOGGER.info("Person logged in");
		this.isLoggedIn = true;
	}

	/**
	 * Assert the logged-out status
	 */
	public void loggedOut() {
		MYLOGGER.info("Person logged out");
		this.isLoggedIn = false;
	}

	/**
	 * Checks whether the person is logged in or not
	 * 
	 * @return a boolean that tells us if the person is logged in or not
	 */
	public boolean status() {
		return isLoggedIn;
	}

	/**
	 * Overrides the default toString method
	 * 
	 * @return Person's name
	 */
	public String toString() {
		return this.getLastName() + ", " + this.getFirstName();
	}

	/**
	 * Sets the password for this user.
	 * 
	 * @param encryptPass
	 *            - encrypted password
	 * 
	 * @return Person's name
	 */
	public void setUserPass(String encryptPass) {
		this.userPass = encryptPass;
	}

	public String getUserPass() {
		return userPass;
	}

	public void setPersonType(UserType personType) {
		this.personType = personType;
	}

	public UserType getPersonType() {
		return personType;
	}
}
